﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQuery;
using mshtml;

namespace Ikariam.MQuery
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class MQueryTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        HTMLDocumentClass document;

        [TestInitialize]
        public void TestInitialize()
        {
            document = MQueryUnitTest.Load("http://localhost/JQueryTest1.htm");
        }

        [TestCleanup]
        public void TestCleanup()
        {
            MQueryUnitTest.Quit();
        }
        #region Selectors
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_ID()
        {
            string id = "DivID1";
            Query query = new Query("#" + id, document);
            Assert.AreEqual(query.Length, 1);
            Assert.IsInstanceOfType(query[0], typeof(IHTMLElement));
            Assert.AreEqual(query.Attribute("ID"), id);
            return;
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_ID_NotExistingID_ReturnsLenghtZeroResult()
        {
            string id = "NotExistingID";
            Query query = new Query("#" + id, document);
            Assert.AreEqual(query.Length, 0);
            return;
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_Tag()
        {
            string tag = "Div";
            Query query = new Query(tag, document);
            Assert.AreEqual(query.Length, document.getElementsByTagName(tag).length);
            foreach( var  element in query )
            {
                Assert.IsTrue( string.Compare(((IHTMLElement)element).tagName,tag,true) == 0 );
            }
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_TagTag()
        {
            string selector = "#DivID3 B";
            Query query = new Query(selector, document);
            Assert.AreEqual(query.Length, 5);
            foreach (var element in query)
            {
                Assert.IsTrue(string.Compare(((IHTMLElement)element).tagName, "b", true) == 0);
            }
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_Class()
        {
            string className = "Class2";
            Query query = new Query("." + className, document);
            Assert.AreEqual(2,query.Length);
            foreach (var element in query)
            {
                Assert.IsTrue(element.HasClass(className));
            }
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_Class_MultiClass()
        {
            string className = "Class3";
            Query query = new Query("." + className, document);
            Assert.AreEqual(3, query.Length);
            foreach (var element in query)
            {
                Assert.IsTrue(element.HasClass(className));
            }
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Selector_IdClass()
        {
            string selector = "#DivID2 .Class2";
            Query query = new Query(selector, document);
            Assert.AreEqual(1,query.Length);
            foreach (var element in query)
            {
                Assert.IsTrue(element.HasClass("Class2"));
            }
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Filter_Eq()
        {
            string selector = "div:eq(6)";
            Query query = new Query(selector, document);
            Assert.AreEqual(1, query.Length);
            Assert.AreEqual(query.Attribute("ID"), "DivID7");
        }
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Filter_First()
        {
            string selector = "div:first";
            Query query = new Query(selector, document);
            Assert.AreEqual(1, query.Length);
            Assert.AreEqual(query.Attribute("ID"), "DivID1");
        }
        #endregion Selectors
        #region Methods

        [Description("Attribute returns the attirube value of the first element of the query")]
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Attribute_SimpleScenario_ReturnsAttributesOfTheFirstElement()
        {
            string id = "DivID1";
            Query query = new Query("#" + id, document);
            Assert.AreEqual(query.Attribute("ID"), id);
            return;
        }

        [Description("Attribute call for zero lenght query MUST return null")]
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Attribute_ZeroLenghtQuery_ReturnsAttributesOfTheFirstElement()
        {
            string id = "NotExistingID";
            Query query = new Query("#" + id, document);
            Assert.IsNull(query.Attribute("Any"));
            return;
        }

        [Description("Text returns the inner text of the first element of the query")]
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Text_SimpleScenario_ReturnsTheInnerTextOfTheFirstElement()
        {
            string id = "DivID1";
            Query query = new Query("#" + id, document);
            Assert.AreEqual(query.Text(), query[0].innerText);
            return;
        }

        [Description("Text call for zero lenght query MUST return null")]
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Text_ZeroLenghtQuery_ReturnsAttributesOfTheFirstElement()
        {
            string id = "NotExistingID";
            Query query = new Query("#" + id, document);
            Assert.IsNull(query.Text());
            return;
        }

        [Description("Each MUST iterate over all selected elements. Index must be consistent")]
        [Owner("Yehuda Adar")]
        [TestMethod]
        public void Each_SimpleScenario_IterateOnEachElement()
        {
            string selector = "Div B";
            Query query = new Query(selector, document);
            int count = 0;

            query.Each((index, element) =>
            {
                Assert.AreEqual(query[index], element);                
                count++;
            });

            Assert.AreEqual(query.Length, count);
        }
        #endregion Methods
    }
}
